package com.mediacloud.app.newsmodule.fragment.topic

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.mediacloud.app.fbnavsk.navPaddingTopMatch
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.ArticleListData
import com.mediacloud.app.model.news.CatalogItem
import com.mediacloud.app.nav2.fragment.HqyNavFragment
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.adaptor.topic.TopicAdapter
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
import com.mediacloud.app.newsmodule.utils.ToastUtil
import com.mediacloud.app.reslib.util.DataInvokeUtil
import com.mediacloud.app.user.utils.LogUtil
import com.mediacloud.app.user.utils.RxUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.fragment_topic_new.*


/**
 * 新增的集中专题样式
 * */
class TopicFragmentX : HqyNavFragment(), OnRefreshLoadMoreListener {

    var catalogItem: CatalogItem? = null

    var catalogID: String? = null

    var page = 1
    var pageSize = 10

    var mAdapter: TopicAdapter? = null

    override fun getLayoutResID(): Int {
        return R.layout.fragment_topic_new
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        navPaddingTopMatch()
    }

    override fun initView() {
        setRecycler()

        getNavContentList(catalogID + "", page, pageSize)
    }

    private fun setRecycler() {
        refreshLayout.setOnRefreshLoadMoreListener(this)
        recycler_topic.layoutManager = LinearLayoutManager(activity)
        when (catalogItem?.catalogStyle) {
            3 -> {
                mAdapter = TopicAdapter(R.layout.item_topic_style3)
            }

            4 -> {
                mAdapter = TopicAdapter(R.layout.item_topic_style4)
            }

            5 -> {
                mAdapter = TopicAdapter(R.layout.item_topic_style5)
            }

            else -> {
                mAdapter = TopicAdapter(R.layout.item_topic_style3)
            }
        }
        mAdapter?.bindToRecyclerView(recycler_topic)
        mAdapter?.setOnItemClickListener { adapter, view, position ->

            val item = adapter.getItem(position) as ArticleItem
            NewsItemClickUtils.OpenItemNewsHandle(activity, item.type, item, CatalogItem())

        }
    }

    override fun initArgs() {
        super.initArgs()
        if (fragmentArguments != null) {
            saveInstanceArguments = fragmentArguments
            catalogID = saveInstanceArguments?.getString("id")
            catalogItem = saveInstanceArguments?.getParcelable("catalog")
        } else if (saveInstanceArguments != null) {
            catalogID = saveInstanceArguments?.getString("id")
            catalogItem = saveInstanceArguments?.getParcelable("catalog")
        }

    }


    @SuppressLint("CheckResult")
    fun getNavContentList(navId: String, pageNumber: Int, pageSize: Int) {
        var result = DataInvokeUtil.getZiMeiTiApi()
            .getContentList(navId, pageNumber, pageSize, DataInvokeUtil.ClientFlag)
        result.map {
            val articleListData = ArticleListData()
            articleListData.readFromJson(it)
            return@map articleListData
        }.compose(RxUtils.schedulersTransformer()).subscribe(
            {
                LogUtil.logw("AppFac", "$it")
                closeStateView()
                refreshLayout.finishRefresh()
                refreshLayout.finishLoadMore()
                if (it.state) {
                    val dataList = it.articleList
                    val component = it.componentItems
                    if (dataList.isNotEmpty()) {
                        if (page == 1) {
                            mAdapter?.setNewData(dataList)
                        } else {
                            mAdapter?.addData(dataList)
                        }
                    }
                } else {
                    ToastUtil.show(activity, "拉取数据失败")
                }
            },
            {
                LogUtil.logw("AppFac", "$it")
                closeStateView()
                refreshLayout.finishRefresh()
                refreshLayout.finishLoadMore()
                ToastUtil.show(activity, "拉取数据失败")
            })
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        page = 1
        getNavContentList(catalogID + "", page, pageSize)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        page++
        getNavContentList(catalogID + "", page, pageSize)
    }
}